package com.hq.ims.data.service;

import com.hq.ims.data.dao.PermissionMapAuthorityMenuMapper;
import com.hq.ims.data.entity.PermissionAuthority;
import com.hq.ims.data.entity.PermissionMapAuthorityMenu;
import com.hq.ims.data.entity.PermissionMenu;
import com.hq.ims.data.utils.ErrMsgConstant;
import com.hq.ims.data.vo.PermissionMapAuthorityMenuMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.utils.util.ListUtils;
import com.hq.utils.util.ObjectUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import lombok.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * jiang bai ping
 *
 * @Email 526666031@qq.com
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BasePermissionMapAuthorityMenuService extends BaseService<PermissionMapAuthorityMenuMapper, PermissionMapAuthorityMenu> {

    @Resource
    private BasePermissionAuthorityService basePermissionAuthorityService;

    @Resource
    private BasePermissionMenuService basePermissionMenuService;

    /**
     * 校验映射关系是否重复添加
     */
    private boolean isExists(
            @NonNull String authId,
            @NonNull String menuId
    ) {
        return lambdaQuery()
                .eq(PermissionMapAuthorityMenu::getPermissionMenuId, menuId)
                .eq(PermissionMapAuthorityMenu::getPermissionAuthorityId, authId)
                .count() > 0;
    }

    /**
     * 绑定权限菜单
     */
    @Transactional(rollbackFor = Exception.class)
    public void bindAuthorityMenu(PermissionMapAuthorityMenuMapstruct.BindAuthorityMenuVo saveVo) {
        SpringBootValidateUtils.validate(saveVo);

        PermissionAuthority permissionAuthority = basePermissionAuthorityService.getAuthByAuthIdAndCompanyId(saveVo.getPermissionAuthorityId(), saveVo.getCompanyId());
        ObjectUtils.isNullThrow(permissionAuthority, ErrMsgConstant.PERMISSION_AUTH_NOT_EXISTS);

        //删除现在的权限和菜单绑定
        this.delByAuthId(saveVo.getPermissionAuthorityId());
        //重新添加
        List<PermissionMapAuthorityMenu> saveList = new ArrayList<>();
        List<String> menuIdList = saveVo.getPermissionMenuIdList().stream().distinct().collect(Collectors.toList());
        for (String menuId : menuIdList) {
            PermissionMapAuthorityMenu permissionMapRoleMenu = new PermissionMapAuthorityMenu();
            saveList.add(permissionMapRoleMenu);
            permissionMapRoleMenu.setPermissionMenuId(menuId);
            permissionMapRoleMenu.setPermissionAuthorityId(saveVo.getPermissionAuthorityId());
        }
        if (ListUtils.isExistsEle(saveList)) {
            this.saveBatch(saveList);
        }
    }

    /**
     * 绑定菜单权限
     */
    @Transactional(rollbackFor = Exception.class)
    public void bindMenuAuthority(PermissionMapAuthorityMenuMapstruct.BindMenuAuthorityVo saveVo) {
        SpringBootValidateUtils.validate(saveVo);

        PermissionMenu permissionMenu = basePermissionMenuService.getById(saveVo.getPermissionMenuId());
        ObjectUtils.isNullThrow(permissionMenu, ErrMsgConstant.PERMISSION_MENU_NOT_EXISTS);

        //删除现在的权限和菜单绑定
        this.delByMenuId(saveVo.getPermissionMenuId());
        //重新添加
        List<PermissionMapAuthorityMenu> saveList = new ArrayList<>();
        List<String> authIdList = saveVo.getPermissionAuthorityIdList().stream().distinct().collect(Collectors.toList());
        for (String authId : authIdList) {
            PermissionMapAuthorityMenu permissionMapRoleMenu = new PermissionMapAuthorityMenu();
            saveList.add(permissionMapRoleMenu);
            permissionMapRoleMenu.setPermissionMenuId(saveVo.getPermissionMenuId());
            permissionMapRoleMenu.setPermissionAuthorityId(authId);
        }
    }

    /**
     * 根据角色Id删除相应映射
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByAuthId(String authId) {
        this.lambdaUpdate().eq(PermissionMapAuthorityMenu::getPermissionAuthorityId, authId).remove();
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByAuthIdList(List<String> authIdList) {
        this.lambdaUpdate().in(PermissionMapAuthorityMenu::getPermissionAuthorityId, authIdList).remove();
    }

    /**
     * 删除菜单根据菜单id
     *
     * @param menuId 菜单ID
     */
    public boolean delByMenuId(String menuId) {
        return this.lambdaUpdate().eq(PermissionMapAuthorityMenu::getPermissionMenuId, menuId).remove();
    }

    public boolean delByMenuIdList(List<String> menuIdList) {
        return this.lambdaUpdate().eq(PermissionMapAuthorityMenu::getPermissionMenuId, menuIdList).remove();
    }


    /**
     * 得到所有菜单id通过角色id
     *
     * @param authIdList 权阴id
     */
    public List<String> getMenuIdListByAuthIdList(List<String> authIdList) {
        if (ListUtils.isNotExistsEle(authIdList)) {
            return new ArrayList<>(0);
        }
        List<PermissionMapAuthorityMenu> roleMenus = this.lambdaQuery()
                .select(PermissionMapAuthorityMenu::getPermissionMenuId)
                .in(PermissionMapAuthorityMenu::getPermissionAuthorityId, authIdList)
                .orderByAsc(PermissionMapAuthorityMenu::getCreateDateTime)
                .list();
        if (ListUtils.isNotExistsEle(roleMenus)) {
            return new ArrayList<>(0);
        }
        return roleMenus.stream().map(PermissionMapAuthorityMenu::getPermissionMenuId).distinct().collect(Collectors.toList());
    }
}
